<?php
namespace Admin\Controller;
use Think\Controller;
class GroupController extends CommonController {
    public function indexAction(){
    	//查询数据库数据条数
    	$sql = "select count(id) count from auth_group";

        $count = current(M()->query($sql))['count'];

        // 分页类
        $Page  = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show();// 分页显示输出

        //where id in ({$value['rules']},对应id在表里面的意思，$str = '';定义一个空字符串
    	// 这里要将auth_group表里具体的规则显示出对应的规则名，但是auth_group表里的规则只有个rules下面对应的是数字，具体的规则在auth_rule表里的title,name具体说明规则情况，现在要将具体的规则说明显示在页面上，var_dump($res[$key])的结果是'id'，'title'，'status'，'rules' ，要单独把每一条的'rules' 取出来，连在一起,自定义rulesstr存放连起来的字符串数据
        $sql = "select * from auth_group  limit {$Page->firstRow},{$Page->listRows}";

    	$res = M()->query($sql);        
    	
    	//&$value传址赋值，只改变数据的存储地址，并没有改变数据的值，将数据a传到值为b的地址中，那么现在数据a的地址为b，值还是a,foreach ($res as &$value)循环$res，将它每一项传址给$value使用，并没有改变原来数组$res的键和值，要在foreach循环外面将新数据追加给数组，或变量，而不是循环里面
    	foreach ($res as &$value) {
    		$sql = "select title from auth_rule where id in ({$value['rules']})";

    		$arr = M()->query($sql);    		

    		$str = '';

    		foreach ($arr as $key => $v) {
    			
    			$str .= $v['title'].',';
    			
    		}
    		$value['rulesstr'] = rtrim($str,',');
    	}           

        $this->assign('count',$count);
        $this->assign('res',$res);
        $this->assign('show',$show);
        $this->display();
    }

    public function addAction(){
    	if (IS_POST) {
    		// var_dump($_POST);
    		$rolegroup = $_POST['rolegroup'];

            $rolesql = "select title from auth_group where title='{$rolegroup}'";
            $roleres = M()->query($rolesql);
            if ($roleres) {
                $this->error('该组已存在',U('admin/group/index'));
            }

    		$rules = $_POST['rules'];

            if (empty($rules)) {
                $this->error('添加失败，请勾选内容',U('admin/group/index'));
            }
    		$rulesstr = implode(',', $rules);
    		$sql = "insert into auth_group (title,rules) value ('{$rolegroup}','{$rulesstr}')";
    		if (M()->execute($sql)) {
    			$this->success('添加成功',U('admin/group/index'));
    		}else{
    			$this->error('添加失败',U('admin/group/index'));
    		}
    	}else{
    		$sql = "select * from auth_rule_category";
	    	$categoryData = M()->query($sql);
    		foreach ($categoryData as &$v) {
	    		$sql = "select * from auth_rule where cid = '{$v['id']}'";
    			$v['son'] = M()->query($sql);
    			$str = '';
    		}
	    	$this->assign('categoryData',$categoryData);
        	$this->display();
    	}
    }

    public function editAction(){    	
    	if(IS_POST){
    		var_dump($_POST);
    		$rolegroup = $_POST['rolegroup'];
    		$rules = $_POST['rules'];
    		$id = $_POST['id'];
    		$rulesstr = implode(',', $rules);
    		$sql = "update auth_group set title='{$rolegroup}',rules='{$rulesstr}' where id='{$id}'";
    		// var_dump($sql);exit;
    		if (M()->execute($sql)) {
    		 	$this->success('修改成功',U('admin/group/index'));
    		}else{
    		 	$this->error('修改失败',U('admin/group/index'));
    		}

    	}else{

	    	$id = I('get.id','');
	    	if (empty($id)) {
				$this->error('非法请求',U('admin/group/index'));
			}else{
				$sql = "select * from auth_group where id='{$id}'";
				$res = current(M()->query($sql));
		    	$sqlrule = "select * from auth_rule_category";
	    		$categoryData = M()->query($sqlrule);
	    		foreach ($categoryData as &$v) {
		    		$sql = "select * from auth_rule where cid = '{$v['id']}'";
	    			$v['son'] = M()->query($sql);
	    			$str = '';
	    		}
		    	$this->assign('categoryData',$categoryData);
		    	$this->assign('res',$res);
		    	$this->display();
	    		
	    	}
    	}
    }

    public function delAction(){
    	$id = I('get.id');

        $sql = "delete from  auth_group where id='{$id}'";

        $res = M()->execute($sql);

        if($res){
            $this->success('删除成功',U('admin/group/index'));
        }else{
            $this->error('删除失败',U('admin/group/index'));
        }
    }


//ajax的异步删除方法
    // public function delAction(){
    //     $id = I('post.thegroupid');
    //     $sql = "delete from  auth_group where id='{$id}'";
    //     M()->execute($sql);
    //     json_encode(1);
    // }
    
    public function piliangdelAction() {
        var_dump($_POST);
    }
}